package com.jiantong.web.open.app.work;

import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.BreedTransferRecord;
import com.jiantong.work.service.BreedTransferRecordReadService;
import com.jiantong.work.service.BreedTransferRecordWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.Map;

/**
 * Created by mz on 14/8/17.
 * 转群的对外接口
 */
@Slf4j
@OpenBean
public class BreedTransferRecordApis {

    @RpcConsumer
    private BreedTransferRecordReadService breedTransferRecordReadService;

    @RpcConsumer
    private BreedTransferRecordWriteService breedTransferRecordWriteService;

    @OpenMethod(
            key = "find.breed.transfer.record.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BreedTransferRecord findById(Long id){

        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"id should be not null");
        }

        Response<BreedTransferRecord> recordResponse = breedTransferRecordReadService.findById(id);

        if(!recordResponse.isSuccess()){
            log.error("failed to find breed transfer record by id , case = {}",recordResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),recordResponse.getError());
        }

        return recordResponse.getResult();
    }

    @OpenMethod(
            key = "paging.breed.transfer.record",
            paramNames = {"pageNo","pageSize","breedFactoryId","fromStableId","toStableId","fromDate","toDate","status"},
            httpMethods = RequestMethod.GET
    )
    public Paging<BreedTransferRecord> paging(Integer pageNo , Integer pageSize,
                                              Long breedFactoryId , Long fromStableId,
                                              Long toStableId,String fromDate,
                                              String toDate,Integer status){

        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"breedFactoryId should be not null");
        }

        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
           start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }

        Response<Paging<BreedTransferRecord>> response =
                breedTransferRecordReadService.paging(pageNo,pageSize,breedFactoryId,fromStableId,toStableId,start,end,status);

        if(!response.isSuccess()){
            log.error("failed to paging breed transfer record , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "create.breed.transfer.record",
            paramNames = {"breedTransferRecord","toStatus"},
            httpMethods = RequestMethod.POST
    )
    public Long create(BreedTransferRecord breedTransferRecord , Integer toStatus){
        if(breedTransferRecord == null){
            log.error("breedTransferRecord should be not null");
            throw new OPClientException(ExceptionCode.OBJECT_NOT_EXIST.value(),"breedTransferRecord should be not null");
        }

        Response<Long> response = breedTransferRecordWriteService.create(breedTransferRecord,toStatus);

        if(!response.isSuccess()){
            log.error("failed to create breed transfer record , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "update.breed.transfer.record",
            paramNames = {"breedTransferRecord","toStatus"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(BreedTransferRecord breedTransferRecord , Integer toStatus){
        if(breedTransferRecord == null){
            log.error("breedTransferRecord should be not null");
            throw new OPClientException(ExceptionCode.OBJECT_NOT_EXIST.value(),"breedTransferRecord should be not null");
        }
        if(breedTransferRecord.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"id should be not null");
        }

        Response<Map<String,Integer>> mapResponse = breedTransferRecordReadService.findStatusById(breedTransferRecord.getId());

        if(!mapResponse.isSuccess()){
            log.error("this report may be had some issues , case = {}",mapResponse.getError());
            throw new OPServerException("this report may be had some issues");
        }else if(mapResponse.isSuccess() && mapResponse.getResult().get("status").equals(toStatus)){
            log.error("this report has already submitted");
            throw new OPServerException(ExceptionCode.DOCUMENT_REPEAT_SUBMIT.value(),"this report has already submitted");
        }

        Response<Boolean> response = breedTransferRecordWriteService.update(breedTransferRecord,toStatus);
        if(!response.isSuccess()){
            log.error("failed to update breed transfer record , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),response.getError());
        }

        return response.getResult();
    }
}
